package com.llmcu.msb.architect.concurrent.basic;

import java.util.concurrent.*;

/**
 * 创建线程的5种方法
 *
 * @author liuling
 * @date 2024/9/16 23:19
 */
public class HowToCreateThread {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        new MyThread().start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("2.通过实现Runnable创建线程");
            }
        }).start();
        new Thread(() -> System.out.println("3.通过lambda表达式创建线程")).start();

        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(() -> System.out.println("4.通过线程池创建线程"));

        Future<String> future = executorService.submit(() -> "5.线程池与Callable");
        String result = future.get();
        System.out.println(result);

        FutureTask<String> futureTask = new FutureTask<>(() -> "5.线程与Callable");
        new Thread(futureTask).start();
        System.out.println(futureTask.get());

        executorService.shutdown();
    }

    static class MyThread extends Thread {
        @Override
        public void run() {
            System.out.println("1.通过继承Thread创建线程");
        }
    }
}
